// Declare utility helper class
Flair.Utility = {};

Flair.Utility.newWindow = function (path, params){
	// define default params
	var options = Flair.extend({
		transparent: true,
		systemChrone: false,
		scrollbars: true,
		visible: true,
		data: {},
		html: ''
	}, params);
	
	if(!path && !options.html){
		Flair.log("No file specified for new window, and HTML content is empty string.");
	}
	
	var initOptions = new air.NativeWindowInitOptions();
			
	//transparent windows must have the systemChrome set to none
	initOptions.transparent = options.transparent;
	if(options.systemChrome)
		initOptions.systemChrome = air.NativeWindowSystemChrome.STANDARD;
	else initOptions.systemChrome = air.NativeWindowSystemChrome.NONE;
			
	var visibleBounds = air.Screen.mainScreen.visibleBounds;
		
	
	var bounds = new air.Rectangle(	options.left || visibleBounds.left, 
									options.top || visibleBounds.top, 
									options.width || visibleBounds.width, 
									options.height || visibleBounds.height );
		
	Flair.log('bounds:', bounds);
	
	var htmlLoader = air.HTMLLoader.createRootWindow( 
		options.visible, // window visible?
		initOptions, // NativeWindowInitOptions 
		options.scrollbars // scrollbars?
	);

	/*	
	var onComplete = function (){
			htmlLoader.removeEventListener("layoutComplete", onComplete);
			htmlLoader.window.nativeWindow.visible = true;
			htmlLoader.window.nativeWindow.x = 300;
			htmlLoader.window.nativeWindow.y = 200;
	}
	htmlLoader.addEventListener("layoutComplete", onComplete);	
	*/	
	
	if(bounds != undefined) {
		htmlLoader.window.nativeWindow.bounds = bounds;
	}
	htmlLoader.window.nativeWindow.alwaysInFront = true;
	
	// Make the body tag transparent
	htmlLoader.paintsDefaultBackground = ! options.transparent;

	htmlLoader.addEventListener(air.Event.HTML_DOM_INITIALIZE, function(){
		// Inject the opener, maybe for communication purposes 
		htmlLoader.window.opener = window;
	});
			
	if(options.data) 
		htmlLoader.window.data = options.data;
	
	if(path)
		htmlLoader.load( new air.URLRequest(path) );  
	else
		htmlLoader.loadString( options.html );
		
	// todo: wait till oncomplete .. 
	htmlLoader.window.nativeWindow.visible = true;

	return htmlLoader;
};